import { createRouter } from 'vue-router';
import type  {Iroute} from "@/mytype/index"
 

/* 通过权限动态获取路由 */

import type { RouteRecordName, Router, RouteRecordRaw } from "vue-router"



let menu:Array<Iroute> = [
    {
        label: "系统首页",
        key: "0-0",
        path: "",
        meta: {
            title: "首页"
        }
    },
    {
        label: "轮播图管理",
        key: "0-1",
        children: [
            {
                label: "轮播图列表",
                key: "0-1-0",
                path: "Banner",
                name: "Banner",
                meta: {
                    title: "轮播图列表"
                },
                component: () => import ("@/views/Banner/BannerList.vue")
            }
        ]
    },
    {
        label: "产品管理",
        key: "0-2",
        children: [
            {
                label: "产品列表",
                key: "0-2-0",
                path: "proList",
                name: "proList",
                meta: {
                    title: "产品列表"
                },
                component: () => import ("@/views/pro/proList.vue")
            },
            {
                label: "秒杀列表",
                key: "0-2-1",
                path: "SecondsKill",
                name: "SecondsKill",
                meta: {
                    title: "秒杀列表"
                },
                component: () => import ("@/views/pro/SecondsKill.vue")
            },
            {
                label: "推荐列表",
                key: "0-2-2",
                path: "RecommendList",
                name: "RecommendList",
                meta: {
                    title: "推荐列表"
                },
                component: ()=>import("@/views/pro/RecommendList.vue")
            },
            {
                label: "筛选列表",
                key: "0-2-3",
                path: "SearchList",
                name: "SearchList",
                meta: {
                    title: "筛选列表"
                },
                component: () => import("@/views/pro/SearchList.vue")
            }
        ]
    },
    {
        label: "账户管理",
        key: "0-3",
        children: [
            {
                label: "用户列表",
                key: "0-3-0",
                path: "userList",
                name: "userList",
                meta: {
                    title: "用户列表"
                },
                component: () => import ("@/views/User/userList.vue")
            },
            {
                label: "管理员列表",
                key: "0-3-1",
                path: "adminList",
                name: "adminList",
                meta: {
                    title: "管理员列表"
                },
                component: () => import("@/views/Admin/adminList.vue")
            }
        ]
    },
    {
        label: "数据可视化",
        key: "0-4",
        children: [
            {
                label: "echarts使用示例",
                key: "0-4-0",
                path: "echarts",
                name: "echarts",
                meta: {
                    title: "echarts使用示例"
                },
                component: () => import ("@/views/Datavisual/Echarts.vue")
            },
            {
                label: "antv-g2使用示例",
                key: "0-4-1",
                path: "antvg2",
                name: "antvg2",
                meta: {
                    title: "antv-g2使用示例"
                },
                component: () => import("@/views/Datavisual/Antvg2.vue")
            },
            {
                label: "antdcharts",
                key: "0-4-2",
                path: "antdCharts",
                name: "antdCharts",
                meta: {
                    title: "antdcharts"
                },
                component: () => import("@/views/Datavisual/AntdCharts.vue")
            },
        ]
    },
    {
        label: "编辑器",
        key: "0-5",
        children: [
            {
                label: "富文本编辑器",
                key: "0-5-0",
                path: "RichText",
                name: "RichText",
                meta: {
                    title: "富文本编辑器"
                },
                component: () => import ("@/views/Edit/RichText.vue")
            },
            {
                label: "Markdown",
                key: "0-5-1",
                path: "Markdown",
                name: "Markdown",
                meta: {
                    title: "Markdown"
                },
                component: () => import("@/views/Edit/Markdown.vue")
            },
        ]
    },
    {
        label: "导入以及导出",
        key: "0-6",
        children: [
            {
                label: "导入excel",
                key: "0-6-0",
                path: "ImportExcel",
                name: "ImportExcel",
                meta: {
                    title: "导入excel"
                },
                component: () => import ("@/views/importExport/ImportExcel.vue")
            },
            {
                label: "导出excel",
                key: "0-6-1",
                path: "ExportExcel",
                name: "ExportExcel",
                meta: {
                    title: "导出excel"
                },
                component: () => import("@/views/ImportExport/ExportExcel.vue")
            },
        ]
    },
    {
        label: "使用地图",
        key: "0-7",
        children: [
            {
                label: "百度地图",
                key: "0-7-0",
                path: "BaiDuMap",
                name: "BaiDuMap",
                meta: {
                    title: "百度地图"
                },
                component: () => import ("@/views/UseMap/BaiDuMap.vue")
            },
            {
                label: "高德地图",
                key: "0-7-1",
                path: "GaoDeMap",
                name: "GaoDeMap",
                meta: {
                    title: "高德地图"
                },
                component: () => import("@/views/UseMap/GaoDeMap.vue")
            },
            {
                label: "腾讯地图",
                key: "0-7-2",
                path: "TenXunMap",
                name: "TenXunMap",
                meta: {
                    title: "腾讯地图"
                },
                component: () => import("@/views/UseMap/TenXunMap.vue")
            },
        ]
    },
    {
        label: "客服",
        key: "0-8",
        path: "",
        meta:{}
    }
]

/* 根据权限筛选当前用户的菜单和路由配置*/
export function getMenu (checkedkeys:Array<string>, $router:Router):Array<Iroute>{
    /* 判断是不是超级管理员 */
    if (checkedkeys.length == 0){
        checkedkeys = [
        "0-0", 
        "0-1", "0-1-0",
        "0-2", "0-2-0", "0-2-1", "0-2-2", "0-2-3", 
        "0-3", "0-3-0", "0-3-1", 
        "0-4", "0-4-0", "0-4-1", "0-4-2",
        "0-5", "0-5-0", "0-5-1",
        "0-6", "0-6-0", "0-6-1",
        "0-7", "0-7-0", "0-7-1",
        "0-8"
    ]
    }

    let resultList:Array<Iroute> = [];
    menu.forEach((item:Iroute)=>{
        if(checkedkeys.includes(item.key)){
            resultList.push({...item});

            /* 判断是不是有子菜单 */
            if (item.children) {
                resultList[resultList.length - 1].children = new Array<Iroute>();
                item.children.forEach((subItem:Iroute)=>{
                    resultList[resultList.length - 1].children?.push({...subItem})

                    /* 动态添加路由：路由对象.addRoute(父级路由的名字，子路由配置) */

                    $router.addRoute(("home" as RouteRecordName), (subItem as RouteRecordRaw) )
                })
            } else {
                $router.addRoute(("admin" as RouteRecordName), (item as RouteRecordRaw))
            }
        }
    })


    return resultList
}





